class AddPagePositions < ActiveRecord::Migration
  def self.up
    add_column :pages, :position, :integer if !Page.new.has_attribute?('slug')
    Page.reset_column_information
    
    Page.find_by_name('Home').update_attribute('position', 1)
    
    position = 2
    for page in Page.find(:all, :conditions => ["parent_id is null AND name != ?", 'Home'])
      page.update_attribute('position', position)
      Page.reset_child_positions(page)
      position = position + 2
    end
  end

  def self.down
    remove_column :pages, :position
  end
end
